package com.fu.sso.controller;

import com.fu.core.common.JsonResult;
import com.fu.core.pojo.User;
import com.fu.sso.service.UserLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author 付龙江
 * @date 2022/2/1 20:44
 */
@RestController
@RequestMapping("/sso")
@CrossOrigin
@Api(tags = "前台一键登录/注册")
public class UserLoginController {

    UserLoginService userLoginService;

    @Autowired
    public UserLoginController(UserLoginService userLoginService) {
        this.userLoginService = userLoginService;
    }

    @PostMapping("/login")
    @ApiOperation("登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "code", value = "短信验证码", dataType = "String", paramType = "query", required = true),
    })
    public Map<String, Object> login(String phone, String code) throws Exception {
        String token = userLoginService.login(phone, code);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("登录注册成功！");
        jsonResult.setField("token", token);
        return jsonResult.getMap();
    }

    @PostMapping("/checkToken")
    public Map<String, Object> checkToken(String token) throws Exception {
        User user = userLoginService.checkToken(token);
        System.out.println("user:" + user);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("发送成功");
        jsonResult.setField("data", user);
        return jsonResult.getMap();
    }

    @PostMapping("/send")
    @ApiOperation("发送验证码接口")
    public Map<String, Object> sendSMS(String phone) {
        String s = userLoginService.sendLoginSms(phone);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("发送成功");
        jsonResult.setField("sendCode", s);
        return jsonResult.getMap();
    }
}
